
<{include file="app/header.tpl"}>

	<div id="cart_message" style="display: none;">
		<p class="title">提示框</p>
		<p class="jiaru">是否确定从购物车中移除该商品？</p>
		<p class="content fix">
			<span class="chakan">确定</span>
			<span class="jixu">取消</span>
		</p>
	</div>

	<div class="alert-bg" id="alert_bg"></div>

	<{foreach from=$detailList item="ret"}>
	<form class="cartBox" name="cart<{$ret.shop.shopId}>" action="/m/buy/cart" method="GET">
		<ul>
			<li class="cartBox-li">
				<label class="the-shop-name" >
					<input type="checkbox" name="shopSelected[]" onclick="cart_select_all(this);">
					<span class="shop-name"><{$ret.shop.shopName}></span>
					<em class="back-icon"></em>
				</label>
				<ul class="the-shop-all-goods">
					<{foreach from=$ret.goods item="goods"}>
					<li class="goods-li">
						<em class="closeBtn" cartId="<{$goods.cartId}>" onclick="cart_show_alert_bg(this)"></em>
						<label class="left fix">
							<input type="checkbox" name="goodsSelected[]" >
							<img class="img" src="<{$goods.thumbsrc}>"   alt="">
						</label>
						<div class="middle">
							<input type="hidden" name="cartid[]" value="<{$goods.cartId}>">
							<p class="goods-name"><{$goods.name}></p>
							<p class="money"><span>￥<{$goods.money.price/100|number_format:2}></span><span class="del"><{$goods.origPrice}></span></p>
							<p class="three fix">
								<em class="less" onclick="cartAddQuantity(this, -1)"></em>
                                <input type="number" name="quan_<{$goods.id}>_<{$goods.attr.id}>" class="number" value="<{$goods.money.quantity}>" price="<{$goods.money.price}>" onblur="cart_quantity_changed(this.form)" min="1">
								<em class="add" onclick="cartAddQuantity(this, 1)"></em>
							</p>
						</div>
					</li>
					<{/foreach}>
				</ul>
			</li>
		</ul>

		<div class="bottom">
			<label class="line"  >
				<input type="checkbox" name="" id="" onclick="cart_select_all(this);">
				<span class="all-choose">全选</span>
				<span class="heji">合计：<span class="money cart-money-total">￥23</span></span>
			</label>

			<input type="submit" class="so-jiesuan" name="submit_op" value="立即结算">

		</div>

	</form>
	<{/foreach}>

<script>
$(document).ready(function () {

	$('form').each(function () {
		if (/^cart/.test(this.name))
			cart_quantity_changed(this);
	});

    $('input[type="checkbox"]').change(function () {
        cart_quantity_changed(this.form);
    });

    $('form').submit(function () {
        if (cart_quantity_changed(this) <=  0) {
        	alert('请至少选择一项商品');
            return false;
        }
        else {
            return true;
        }
    });
});

function cart_select_all (item) {

	if (item.checked) {

		$(item.form).find("input[type='checkbox']").each(function() { //遍历所有的name为selectFlag的 checkbox
			this.checked = true;
		});

	} else {
		$(item.form).find("input[type='checkbox']").each(function() { //遍历所有的name为selectFlag的 checkbox
			this.checked = false;
		});
	}

    cart_quantity_changed(item.form);
}

function cart_show_alert_bg(item)
{
	$('#alert_bg').show();
	$('#cart_message').show();

	$('#cart_message').find('.chakan').click(function () {
		var cartId = $(item).attr('cartId');
		window.location.assign('/cart/del/' + cartId);
		return;
	});

	$('#cart_message').find('.jixu').click(function () {
		$('#alert_bg').hide();
		$('#cart_message').hide();
	});
};

function cartAddQuantity (item, n) {
	var currN = $(item).parent().find('input').val();
	var nextN = parseInt(currN) + n;

	if (nextN > 100)
		nextN = 100;

	if (nextN <= 0)
		nextN = 1;

	$(item).parent().find('input').val(nextN);
	var ipt = $(item).parent().find('input')[0];
	cart_quantity_changed(ipt.form);
}

function cart_quantity_changed(formItem)
{
	var total = 0;
	$(formItem).find('input').each(function () {
		if ($(this).attr('name').match(/^quan_/))
		{
			p = $(this).attr('price');
			n = $(this).val();
			money = p * n;

            ckb = $(this).parent().parent().parent().find('input[type="checkbox"]:first');
            if (ckb.is(':checked'))
                total += money;

			//$(this).parent().parent().parent().parent().next().find('span').html('￥ ' + money / 100);
		}
	});

	$(formItem).find('.cart-money-total').html('￥ ' + total / 100);
    return total;
}

</script>


<{include file="app/footer.tpl"}>
